import { mapMutations, mapState } from "vuex";

export const importMixins = {
  props: {
    isPop: {
      type: Boolean,
      default: false
    },
    type: {
      type: Number,
      default: 0 // 0:工资表, 1:考勤:, 2:公积金, 3:扣除
    },
    attribute: {
      type: Array,
      default: () => [] // 可修改的属性
    }
  },
  data() {
    return {
      action: "xxx",
      fileList: [],
      accept: ".xls,.xlsx,.xml",
      loading: false
    };
  },
  computed: {
    ...mapState({
      operationLoading: state => state.setStorage.operationLoading
    })
  },
  methods: {
    ...mapMutations(["setOperationLoading"]),

    /** 导入  */
    onImport() {
      console.log(this.fileList);
      if (this.fileList.length == 0) {
        this.$message({ message: "请先选择文件!", type: "warning" });
        return;
      }
      if (this?.active == 1 && !this.columnsPy?.length) {
        console.log(this.attribute);
        this.isOptionPop = true;
        return;
      }

      this.$refs.upload.submit();
    },

    uploadSectionFile(params) {
      this.loading = true;
      console.log(params);
      let formdata = new FormData();
      formdata.append("ImportFile", params.file);
      this.$emit("import", {
        type: this.type,
        formdata,
        columnsPy: this.getColumnsPy(),
        fn: bol => {
          this.loading = false;
          if (bol) this.onClose();
        }
      });
    },
    getColumnsPy() {
      return this?.active == 1 ? JSON.parse(JSON.stringify(this.columnsPy)) : this.attribute?.map(item => item.namePy) || [];
    },
    onClose() {
      this.$emit("update:isPop", false);
    },
    // 下载模板
    ...{
      executeDown(url) {
        this.$emit("down", { type: this.type });
        // if (url) window.open(url);
      }
    },
    // 文件上传相关
    ...{
      onChange(e) {
        this.fileList = [e];
      }
    }
  }
};
